<#import "include/main.ftl" as m />
<#import "include/table_w_pager_wrapper.ftl" as t />

<@m.main>
	<ul>
		<li><a href="#usuarios">Usuarios</a></li>
	</ul>
	
	<div id="usuarios">
	
		<#if edit_mode="edit">
			<h2>Editar usuario</h2>
		<#else>
			<h2>Crear nuevo usuario</h2>
		</#if>
		
		<#if edit_mode="edit">
			<form method="post" action="update_existing_user.html">
		<#else>
			<form method="post" action="save_new_user.html" onsubmit="return sbt();">
		</#if>
		
		<#if edit_mode = "new">
			<input type="hidden" id="salt" name="salt" value="${salt!}" />
		</#if>
		
		<#if edit_mode="edit">
			<input type="hidden" name="userId" id="userId" value="${user.id}" /> 
		</#if>
		
		<input type="hidden" id="hashedPasswd" name="hashedPasswd" />
		
		<table class="fVert" cellpadding="0" cellspacing="0">
			
			<tr>
				<th>
					<label for="name">Nombre</label>
				</th>
				<th>
					<label for="email">Email</label>
				</th>
				<th>
					<label for="email_confirmed">Conf</label>
				</th>
				<th>
					<label for="locale">Locale</label>
				</th>
				<#if edit_mode="new">
					<th>
						<label for="password">Contraseña</label>
					</th>
				<#else>
					&nbsp;
				</#if>
				<th>
					<label for="rol">Rol</label>
				</th>
				<th>&nbsp;</th>
			</tr>
			
			<tr>
				<td>
					<input type="text" name="name" id="name" value="<#if edit_mode = "edit">${user.name!}</#if>" />
				</td>
				<td>
					<input type="text" name="email" id="email" value="<#if edit_mode = "edit">${user.email!}</#if>" />
				</td>
				<td>
					<input type="checkbox" name="email_confirmed" id="email_confirmed"<#if edit_mode = "edit" && user.emailConfirmed> checked="checked"</#if> />
				</td>
				<td>
					<select name="locale" id="locale">
						<option value="es"<#if edit_mode = "edit" && user.locale="es"> selected="selected"</#if>>ES</option>
						<option value="ru"<#if edit_mode = "edit" && user.locale="ru"> selected="selected"</#if>>RU</option>
						<option value="en"<#if edit_mode = "edit" && user.locale="en"> selected="selected"</#if>>EN</option>
					</select>
				</td>
				<#if edit_mode="new">
					<td>
						<input type="password" name="password" id="password" />
					</td>
				<#else>
					&nbsp;
				</#if>
				<td>
					<select name="rol" id="rol">
						<#list roles as r>
							<option value="${r.tur_role_id}"<#if edit_mode = "edit" && user.role=r.tur_role_id> selected="selected"</#if>>${r.tur_role_name}</option>
						</#list>
					</select>
				</td>
				<td>
					<#if edit_mode="edit">
						<button>Actualizar</button>
					<#else>
						<button>Crear</button>
					</#if>
				</td>
			</tr>
			
		</table>
		</form>
		
		<#if edit_mode="edit">
			<h2>Modificar contraseña</h2>
			
			<form method="post" action="update_user_password.html">
			<input type="hidden" name="userId" id="userId" value="${user.id}" />
			<table class="fVert">
				<tr>
					<th>
						<label for="updPasswd">Contraseña</label>
					</th>
					<th>
						&nbsp;
					</th>
				</tr>
				<tr>
					<td style="text-align: left; width: 100px;">
						<input type="text" name="updPasswd" id="updPasswd" />
					</td>
					<td style="text-align: left;">
						<button>Actualizar</button>
					</td>
				</tr>
			</table>
			</form>
		</#if>
		
	</div>
	<script type="text/javascript">
		function sbt() {
			var hash = calcHash();
			$("#hashedPasswd").val(hash);
			
			var rnd = "";
			var i = $("#password").val().length;
			while(i != 0) {
				rnd += "0";
				i--;
			}
			
			$("#password").val(rnd);
			
			return true;
		}
		
		function calcHash() {
			var value = $("#password").val();
			var salt = $("#salt").val();
			
			var passwd = salt + value + salt;
			
			var shaObj = new jsSHA(passwd, "TEXT");
			return shaObj.getHash("SHA-512", "HEX");
		}
	</script>
</@m.main> 
